package com.greendragon.persistence.dao.impl; 

import java.sql.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.greendragon.persistence.dao.IProductDao;
import com.greendragon.persistence.domain.Product;
import com.greendragon.persistence.enmu.FetchOrderType;
import com.greendragon.persistence.enmu.LockType;


/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-8 下午11:25:20
 * 
 */
public class ProductDaoImpl extends BaseDao<Product> implements IProductDao{
	private static Logger          logger           = Logger.getLogger(ProductDaoImpl.class);
	
	@Override
	public List<Product> getByDate(Date startDate, Date endDate, int offset, int length, LockType lockType, FetchOrderType orderType) {
		String hql = "from Product as p where p.createdAt between ? and ? and"+getLockSql("p", lockType)+" and"+getOrderSql("p", orderType);
		return selectByPage(hql, new Date[]{startDate, endDate}, offset, length);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public long getCountByDate(Date startDate, Date endDate, LockType lockType){
		String hql = "select count(p.id) from Product as p where p.createdAt between ? and ? and"+getLockSql("p", lockType);
		try {
			List rs = getHibernateTemplate().find(hql, new Date[]{startDate, endDate});
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}
}
 